#课堂练习

# #1-创建球类且在指定位置生成
# import pygame,sys

# class MyBallClass(pygame.sprite.Sprite):
#     def __init__(self,imagefile,location):
#         pygame.sprite.Sprite.__init__(self)
#         self.image=pygame.image.load(imagefile)
#         self.rect=self.image.get_rect()
#         self.rect.left,self.rect.top=location

# size=width,height=640,480
# screen=pygame.display.set_mode(size)
# screen.fill([255,255,255])

# img_file="Image\\beach_ball.png"
# balls=[]
# for row in range(0,3):
#     for column in range(0,3):
#         location=[column*180+10,row*180+10]
#         ball=MyBallClass(img_file,location)
#         balls.append(ball)

# for ball in balls:
#     screen.blit(ball.image,ball.rect)
# pygame.display.flip()

# while True:
#     for event in pygame.event.get():
#         if event.type==pygame.QUIT:
#             sys.exit()


# #2-创建球类且在指定位置生成且添加移动方法
# import pygame,sys
# from random import *

# class MyBallClass(pygame.sprite.Sprite):
#     def __init__(self,imagefile,location,speed):
#         pygame.sprite.Sprite.__init__(self)
#         self.image=pygame.image.load(imagefile)
#         self.rect=self.image.get_rect()
#         self.rect.left,self.rect.top=location
#         self.speed=speed

#     def move(self):
#         self.rect=self.rect.move(self.speed)
#         if self.rect.left<0 or self.rect.right>width:
#             self.speed[0]=-self.speed[0]
#         if self.rect.top<0 or self.rect.bottom>height:
#             self.speed[1]-=self.speed[1]

# size=width,height=640,480
# screen=pygame.display.set_mode(size)
# screen.fill([255,255,255])

# img_file="Image\\beach_ball.png"
# balls=[]
# for row in range(0,3):
#     for column in range(0,3):
#         location=[column*180+10,row*180+10]
#         speed=[choice([-2,2]),choice([-2,2])]
#         ball=MyBallClass(img_file,location,speed)
#         balls.append(ball)

# while True:
#     for event in pygame.event.get():
#         if event.type==pygame.QUIT:
#             sys.exit()
#     pygame.time.delay(20)
#     screen.fill([255,255,255])
#     for ball in balls:
#         ball.move()
#         screen.blit(ball.image,ball.rect)
#     pygame.display.flip()


# #3-使用一个动画精灵组
# import sys,pygame
# from random import *

# class MyBallClass(pygame.sprite.Sprite):
#     def __init__(self, image_file,location,speed):
#        pygame.sprite.Sprite.__init__(self)
#        self.image=pygame.image.load(image_file)
#        self.rect=self.image.get_rect()
#        self.rect.left,self.rect.top=location
#        self.speed=speed

#     def move(self):
#         self.rect=self.rect.move(self.speed)
#         if self.rect.left<0 or self.rect.right>width:
#             self.speed[0]-=self.speed[0]
#         if self.rect.top<0 or self.rect.bottom>height:
#             self.speed[1]=-self.speed[1]

# def animate(group):
#     screen.fill([255,255,255])
#     for ball in group:
#         ball.move()
#     for ball in group:
#         group.remove(ball)

#         if pygame.sprite.spritecollide(ball,group,False):
#             ball.speed[0]=-ball.speed[0]
#             ball.speed[1]=-ball.speed[1]

#         group.add(ball)
#         screen.blit(ball.image,ball.rect)
#     pygame.display.flip()
#     pygame.time.delay(20)

# size=width,height=640,480
# screen=pygame.display.set_mode(size)
# screen.fill([255,255,255])
# # img_file='Image\\beach_ball.png'
# img_file='Image\\b_ball_rect.png'
# group=pygame.sprite.Group()

# for row in range(0,3):
#     for column in range(0,3):
#         location=[column*180+10,row*180+10]
#         speed=[choice([-2,2]),choice([-2,2])]
#         ball=MyBallClass(img_file,location,speed)
#         group.add(ball)

# while True:
#     for event in pygame.event.get():
#         if event.type==pygame.QUIT:
#             sys.exit()
#     animate(group)


#统计时间
import sys,pygame
from random import *

# clock=pygame.time.Clock()
# clock.tick(60)
# fps=clock.get_fps()
# print('fps=',fps)

class MyBallClass(pygame.sprite.Sprite):
    def __init__(self, image_file,location,speed):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load(image_file)
        self.rect=self.image.get_rect()
        self.rect.left,self.rect.top=location
        self.speed=speed

    def move(self):
        self.rect=self.rect.move(self.speed)
        if self.rect.left<0 or self.rect.right>width:
            self.speed[0]-=self.speed[0]
        if self.rect.top<0 or self.rect.bottom>height:
            self.speed[1]=-self.speed[1]

def animate(group):
    screen.fill([255,255,255])
    for ball in group:
        ball.move()
    for ball in group:
        group.remove(ball)

        if pygame.sprite.spritecollide(ball,group,False):
            ball.speed[0]=-ball.speed[0]
            ball.speed[1]=-ball.speed[1] 
            
        group.add(ball)
        screen.blit(ball.image,ball.rect)
    pygame.display.flip()

size=width,height=640,480
screen=pygame.display.set_mode(size)
screen.fill([255,255,255])
img_file='Image\\beach_ball.png'
clock=pygame.time.Clock()
group=pygame.sprite.Group()
for row in range(0,2):
    for column in range(0,2):
        location=[column*180+10,row*180+10]
        speed=[choice([-4,4]),choice([-4,4])]
        ball=MyBallClass(img_file,location,speed)
        group.add(ball)

while 1:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            frame_rate=clock.get_fps()
            print("当前的帧率是：",frame_rate)
            sys.exit()

    animate(group)
    clock.tick(30)


